/**************************************************************************************
 *
 * @(#) UserServiceImpl.java
 * @Package com.jgdt.jgdtkasannong_university_gene_backend.service.user.impl
 **************************************************************************************/
package com.jgdt.jgdtkasannong_university_gene_backend.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jgdt.jgdtkasannong_university_gene_backend.common.enums.BusinessCodeEnums;
import com.jgdt.jgdtkasannong_university_gene_backend.common.enums.SystemCodeEnums;
import com.jgdt.jgdtkasannong_university_gene_backend.common.example.custom.BusinessException;
import com.jgdt.jgdtkasannong_university_gene_backend.entity.user.SysUser;
import com.jgdt.jgdtkasannong_university_gene_backend.mapper.user.UserMapper;
import com.jgdt.jgdtkasannong_university_gene_backend.service.user.UserService;
import com.jgdt.jgdtkasannong_university_gene_backend.vo.user.RegisterVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 用户
 * @author: huangAn
 * @createDate: 2025/04/18
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public void checkExistsUser(RegisterVo registerVo) {
        //校验
        if (userMapper.exists(new QueryWrapper<SysUser>().eq("username", registerVo.getUsername()))) {
            throw new BusinessException(SystemCodeEnums.SUCCESS.getKey(), BusinessCodeEnums.USER_REGISTER_ERR_EXIST_USER.getMsg());
        }
    }

    @Override
    public void insertUser(SysUser sysUser) {
        userMapper.insert(sysUser);
    }

    @Override
    public List<SysUser> queryUser(SysUser sysUser) {
        if(ObjectUtils.isEmpty(sysUser)){
            return new ArrayList<>();
        }
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(sysUser.getUsername())) {
            queryWrapper.lambda().eq(SysUser::getUsername, sysUser.getUsername());
        }
        return userMapper.selectList(queryWrapper);
    }
}




